<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Rules\Phone;
use App\Services\LoginService;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class LoginController extends Controller
{
    // 微信网页授权登录
    public function weChatH5Login(Request $request, LoginService $loginService)
    {
        $userinfo = session('wechat.oauth_user');

        $wxUser = $userinfo['default']; // 微信用户信息

        $user = $loginService->weChatH5Login($wxUser);

        // 跳转到登录界面, 登录界面将保存用户信息
        return $this->success($user);
    }

    // 手机验证码登录
    public function yzmLogin(Request $request, SmsService $smsService, LoginService $loginService)
    {
        $request->validate(
            [
                'mobile' => ['required', new Phone()],
                'code' => 'required|size:4',
            ],
            [
                'mobile.unique' => '该手机号码已经绑定账号',
            ]
        );

        // check验证码
        if (!$smsService->verify($request->input('mobile'), $request->input('yzm'))) {
            return $this->failed('验证码不正确');
        }

        $user = User::query()
            ->where('mobile', $request->input('mobile'))
            ->first();

        // 生成并保存令牌
        $user = $loginService->saveToken($user);

        return $this->success($user);
    }

    // 手机号码, 密码登录
    public function defaultLogin(Request $request, LoginService $loginService)
    {
        $request->validate(
            [
                'mobile' => ['required', new Phone()],
                'password' => ['required'],
            ],
            [
                'mobile.required' => '请输入手机号码',
            ]
        );

        $verify = Auth::attempt(['password' => $request->input('password'), 'mobile' => $request->input('mobile')]);
        if (!$verify) {
            return $this->failed('用户名和密码不正确', 401);
        }

        $user = User::query()
            ->where('mobile', $request->input('mobile'))
            ->first();

        // 生成并保存令牌
        $user = $loginService->saveToken($user);

        return $this->success($user);
    }


    // 绑定手机
    public function bindPhone(Request $request, SmsService $smsService)
    {
        // 用户传递过来的验证码和手机号码正确
        $request->validate(
            [
                'mobile' => ['required', new Phone(), 'unique:users'],
                'code' => 'required|size:4',
            ],
            [
                'mobile.unique' => '该手机号码已经绑定账号',
            ]
        );

        $user = $request->user(); // 获取用户,这里可以这样获取用户名,是因为使用了GetUserToken中间件

        if ($user->mobile) {  // 判定用户是否已经绑定手机号
            return $this->failed('已绑定手机');
        }

        // 验证用户的验证码是否正确
        if (!$smsService->verify($request->input('mobile'), $request->input('code'))) {
            return $this->failed('验证码不正确');
        }

        // 手机号绑定用户
        $user->mobile = $request->input('mobile');
        $user->save();

        return $this->created('绑定用户手机成功');
    }

    // 获取手机验证码
    public function getYZM(Request $request, SmsService $smsService)
    {
        $request->validate(
            [
                'mobile' => ['required', new Phone(), 'unique:users'],
            ],
            [
                'mobile.unique' => '该手机号码已经绑定账号',
            ]
        );

        // 判定用户是否已经绑定手机号
        if ($request->user()->mobile) {
            return $this->failed('已绑定手机');
        }

        $code = $smsService->sendSms($request->input('mobile'));

        return $this->success(compact('code'));
    }

    // 重新刷新token令牌
    public function refreshToken(Request $request, LoginService $loginService)
    {
        $refreshToken = $request->input('refresh_token');
        $user = $loginService->refreshApiToken($refreshToken);
        return $this->success($user);
    }
}
